package cn.ac.iscas.controller;


import cn.ac.iscas.dao.entity.ComponentMetadataDO;
import cn.ac.iscas.domain.response.middleplatform.ResponseEntity;
import cn.ac.iscas.service.component.ComponentService;
import com.iscas.datasong.lib.common.DataSongException;
import com.iscas.datasong.lib.common.Status;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author LJian
 * @version 1.0
 * @description: TODO
 * @date 2022/7/26 15:02
 */
//@CrossOrigin(origins = "*", maxAge = 3600, allowCredentials = "true")
@RestController
@RequestMapping({"/component"})
public class ComponentController {
    @Autowired
    private ComponentService componentService;



    /*
     * 获取所有组件
     */
    @RequestMapping(value = "/tree/all", method = RequestMethod.GET)
    ////@ApiOperation(value = "【获取组件树及其所有组件】", notes = "获取组件树及其所有组件")
    public ResponseEntity getComponentTree(@RequestHeader("Authorization") String authorization ) throws DataSongException {
        ResponseEntity responseEntity = new ResponseEntity(Status.OK.getValue(),"获取组件树成功！");
        try {
            responseEntity.setData(componentService.getTypeTree(true, authorization));
        } catch (Exception e){
            e.printStackTrace();
            throw new DataSongException("获取组件树失败！",e);
        }
        return responseEntity;
    }

    /*
     * 根据任务类型下拉框获取组件，如dameng
     */
    @RequestMapping(value = "/tree/type/{taskType}", method = RequestMethod.GET)
    public ResponseEntity getComponentTreeByType(@PathVariable String taskType, @RequestHeader("Authorization") String authorization) throws DataSongException {
        ResponseEntity responseEntity = new ResponseEntity(Status.OK.getValue(),"获取组件树成功！");
        try {
            responseEntity.setData(componentService.getTypeTreeByType(true, taskType, authorization));
        } catch (Exception e){
            e.printStackTrace();
            throw new DataSongException("获取组件树失败！",e);
        }
        return responseEntity;
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    ////@ApiOperation(value = "【获取组件详情】", notes = "根据id号获取组件的详情")
    public ResponseEntity getComponent(@PathVariable String id) throws DataSongException {
        ResponseEntity responseEntity = new ResponseEntity(Status.OK.getValue(),"获取组件详情成功！");
        try{
            ComponentMetadataDO component = componentService.getComponent(id);
            responseEntity.setData(component);
        }catch (DataSongException e){
            e.printStackTrace();

            throw e;
        }catch (Exception e){
            e.printStackTrace();

            throw new DataSongException("获取组件详情！",e);
        }
        return responseEntity;
    }

    /**
     * 获取组件列表 List<Component>
     * @return responseEntity
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
//    @ApiImplicitParams(
//            {
//                    @ApiImplicitParam(name = "type", value = "组件大类", required = false, dataType = "String"),
//                    @ApiImplicitParam(name = "subType", value = "组件子类", required = false, dataType = "String")
//            }
//    )
//    //@ApiOperation(value = "【获取组件列表】", notes = "获取所有组件")
    public ResponseEntity getComponentList(@RequestParam(required = false) String type,  @RequestParam(required = false) String subType) throws DataSongException {
        ResponseEntity responseEntity = new ResponseEntity(Status.OK.getValue(),"get success");
        try{
            List<ComponentMetadataDO> componentMetadata = componentService.getComponentList(type, subType);
            responseEntity.setData(componentMetadata);
        }catch (DataSongException e){
            e.printStackTrace();
            throw e;
        }catch (Exception e){
            e.printStackTrace();

            throw new DataSongException("获取组件列表失败！",e);
        }
        return responseEntity;
    }

    @RequestMapping(value = "/", method = RequestMethod.POST)
    //@ApiOperation(value = "【保存、发布组件】", notes = "提交发布新的组件，需要前台来组装参数viewParameter")
    public ResponseEntity saveComponent(@RequestBody ComponentMetadataDO componentMetadata) throws DataSongException {
        ResponseEntity responseEntity = new ResponseEntity(Status.OK.getValue(),"保存发布成功！");
        try {
            String id = componentService.saveComponent(componentMetadata);
            responseEntity.setData(id);
        }catch (DataSongException e){
            e.printStackTrace();
            throw e;
        }catch (Exception e){
            e.printStackTrace();

            throw new DataSongException("保存/发布组件失败！",e);
        }
        return responseEntity;
    }

    @RequestMapping(value = "/", method = RequestMethod.DELETE)
    //@ApiOperation(value = "【组件撤销/删除】", notes = "删除或撤销已经发布的模型组件,批量的话id用英文逗号分隔，如1,2,3")
//    @ApiImplicitParams(
//            {
//                    @ApiImplicitParam(name = "id", value = "组件模型的_id", required = true, dataType = "String")
//            }
//    )
    public ResponseEntity deleteComponent(@RequestParam  String id) throws DataSongException {
        ResponseEntity responseEntity = new ResponseEntity(Status.OK.getValue(),"组件撤销成功！");
        try {
            responseEntity.setData(componentService.deleteComponent(id));
        }catch (DataSongException e){
            e.printStackTrace();
            throw e;
        }catch (Exception e){
            e.printStackTrace();

            throw new DataSongException("组件撤销失败！",e);
        }
        return responseEntity;
    }

    @RequestMapping(value = "/tree", method = RequestMethod.GET)
    //@ApiOperation(value = "【获取组件树】", notes = "仅生成树的非叶子节点")
    public ResponseEntity getTree(@RequestHeader("Authorization") String authorization ) throws DataSongException {
        ResponseEntity responseEntity = new ResponseEntity(Status.OK.getValue(),"获取组件树成功！");
        try {
            responseEntity.setData(componentService.getTypeTree(false, authorization));
        } catch (Exception e){
            e.printStackTrace();

            throw new DataSongException("获取组件树失败！",e);
        }
        return responseEntity;
    }


}
